package cn.fbs.service;


import cn.fbs.dao.dm.SysUserMapper;
import cn.fbs.dao.dm.UserGroupMapper;
import cn.fbs.model.dm.SysUser;
import cn.fbs.model.dm.UserGroup;
import cn.fbs.util.AESUtils;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Base64;

@Service
public class AuthorityService {

    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private UserGroupMapper userGroupMapper;



    public String login(String loginName, String password) {
        SysUser tUserInfo = userMapper.selectByLoginName(loginName);
        if (tUserInfo==null){
            return "500,用户不存在";
        }
        String myPassword=tUserInfo.getPassword();
        //解码
        String realPass = AESUtils.decryptData(myPassword);
        if (!password.equals(realPass)){
            return "500,用户名或密码错误";
        }

        String dePassword = AESUtils.encryptData(myPassword);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("userName",tUserInfo.getUserName());
        jsonObject.put("loginName",loginName);
        jsonObject.put("password",dePassword);
        jsonObject.put("userRole",tUserInfo.getUserRole());
        jsonObject.put("userId",tUserInfo.getUserId());
        /**
         * 添加roleType
         */
        UserGroup userGroup=userGroupMapper.selectByUserId(tUserInfo.getUserId());
        if (userGroup!=null){
            jsonObject.put("roleType",userGroup.getRoleType());
        }
        String sessionId  = jsonObject.toJSONString();
        return "200,"+Base64.getEncoder().encodeToString(sessionId.getBytes());
    }
}
